#!/bin/sh
#
# file_chooser
#

export TEXTDOMAIN=libstardust
export OUTPUT_CHARSET=UTF-8

case $1 in -h|--help)
	echo "
$0

usage :
 ${0##*/} [options] [Window Title] [text1] [text2] [text3] [text4]

 options:
   --ok-label <text> : yes button label
   --cancel-label <text>  : no button label
   --warning          : warning icon (window)
   --cancel-icon <icon>   : set no button icon
   --ok-icon <icon>  : set yes button icon
   --image-icon <icon>: set dialog icon
   --path             : set initial directory
   --last-path        : use last directory as the initial dir
   --directory        : directory selection (ignore files)

If the select file is valid, the return value is 0
Otherwise its 1"
	exit ;;
esac

#==============================================================

OK_LABEL=$(gettext 'Select')
OK_ICON=ok
CANCEL_LABEL=$(gettext 'Cancel')
CANCEL_ICON=cancel
ICON="file.svg"
DEFAULT_WT="$(gettext 'Select file')"

## options:
for i in "$@" ; do
	case $1 in
		--ok-label) OK_LABEL="$2" ; shift 2 ;;
		--ok-icon)  OK_ICON="$2"  ; shift 2 ;;
		--cancel-label)  CANCEL_LABEL="$2"  ; shift 2 ;;
		--cancel-icon)   CANCEL_ICON="$2"   ; shift 2 ;;
		--path)      XPATH="$2"  ; shift 2 ;;
		--last-path) LAST_PATH=yes ; shift ;;
		--directory) export SDIR=yes ;ICON="directory.svg" ; DEFAULT_WT="$(gettext 'Select directory')" ; shift 1 ;;
		*) break ;; #no more options...
	esac
done

WINDOW_ICON="/usr/share/pixmaps/puppy/${ICON}"

if [ "$1" ] ; then
	HEADING="$1"
else
	HEADING="$DEFAULT_WT"
fi

export LAST_DIR_FILE=$HOME/.config/.file_chooser_ld
if [ "$LAST_PATH" = "yes" ] ; then
	[ -s "$LAST_DIR_FILE" ] && read -r XPATH < "$LAST_DIR_FILE"
fi

if [ "$XPATH" ] ; then
	[ ! -d "$XPATH" ] && XPATH=$(dirname "$XPATH")
	[ ! -d "$XPATH" ] && XPATH=$PWD
else
	XPATH=$PWD
fi

#==============================================================

#			<action when="confirm-overwrite">echo confirm-overwrite</action>
#			<action when="current-folder-changed">echo current-folder-changed</action>
#			<action when="file-activated">echo file-activated</action>
#			<action when="selection-changed">echo selection-changed</action>
#			<action when="update-preview">echo update-preview</action>

export CFILE=/tmp/filechooser_res$$
export FCD=FILE_CHOOSER_DIALOG_$$

function validate_file() {
	SELECTED_FILE="$@"
	if [ "$SDIR" = "yes" ] ; then
		#select directory
		[ -d "$SELECTED_FILE" ] || return 1
	else
		#select file
		[ -d "$SELECTED_FILE" ] && return 1
	fi
	kill $(busybox ps | grep "$FCD" | grep -v grep | awk '{print $1}')
	echo -n "$SELECTED_FILE" > $LAST_DIR_FILE
	echo -n "$SELECTED_FILE" > $CFILE
}
export -f validate_file

#==============================================================

[ "$2" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$2'"</label></text>'
[ "$3" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$3'"</label></text>'
[ "$4" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$4'"</label></text>'
[ "$5" ] && S=$S'<text xalign="0" use-markup="true"><label>"'$5'"</label></text>'

cat << EOF__ >> /tmp/zzfilechooserdialog$$
export ${FCD}='
<window title="${HEADING}" image-name="${WINDOW_ICON}">
	<vbox border-width="10">
		$S
		<chooser>
			<variable>SELECTED_FILE</variable>
			<default>${XPATH}</default>
			<width>600</width>
			<height>400</height>
		</chooser>
		<hbox>
			<button>
				`/usr/lib/gtkdialog/xml_button-icon ${OK_ICON}`
				<label>"${OK_LABEL}"</label>
				<action>validate_file \$SELECTED_FILE</action>
				</button>
			<button>
				`/usr/lib/gtkdialog/xml_button-icon ${CANCEL_ICON}`
				<label>"${CANCEL_LABEL}"</label>
				<action>EXIT:no</action>
			</button>
		</hbox>
	</vbox>
</window>'
EOF__

. /tmp/zzfilechooserdialog$$
retval=`gtkdialog --program=${FCD}`
rm -f /tmp/zzfilechooserdialog$$

#==============================================================

if [ -s "$CFILE" ] ; then
	echo $(< $CFILE)
	rm -f "$CFILE"
	exit 0
else
	exit 1
fi

### END ###
